package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.*;
import com.itheima.service.RoleService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.*;

/**
 * @Program: health_parent
 * @Description:
 * @Author: choulan
 * @Date:2020-11-23 10:31
 **/
@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;
    //分页查询
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return roleService.findPage(queryPageBean);
    }
    //查找权限信息
    @PreAuthorize("hasAuthority('PERMISSION_QUERY')")
    @RequestMapping("/findAuthority")
    public Result findAuthority(){
        try {
            List<Permission> list= roleService.findAuthority();
            return new Result(true,MessageConstant.GET_PERMISSIONLIST_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_PERMISSIONLIST_FAIL);
        }
    }
    //查找菜单
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    @RequestMapping("/findMenu")
    public Result findMenu(){
        try {
            List<Menu> list= roleService.findMenu();
            return new Result(true,MessageConstant.GET_MENU_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }
    //新增角色
    @PreAuthorize("hasAuthority('ROLE_ADD')")
    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer [] authorityIds,Integer [] menuIds){

        try {
            roleService.add(role,authorityIds,menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_ROLE_FAIL);
        }
        return new Result(true,MessageConstant.ADD_ROLE_SUCCESS);
    }

    //查询角色信息
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Role role=roleService.findById(id);
            return new Result(true,MessageConstant.GET_ROLE_SUCCESS,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ROLE_FAIL);
        }
    }
    //查询所有的角色信息
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Role> list=roleService.findAll();
            return new Result(true,MessageConstant.GET_ROLE_LIST_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ROLE_LIST_FAIL);
        }
    }

    //查询角色菜单信息
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer id){
        try {
            List<Integer> list=roleService.findMenuIdsByRoleId(id);
            return new Result(true,MessageConstant.GET_ROLE_MUENUS_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ROLE_MUENUS_FAIL);
        }
    }

    //查询角色权限信息
    @PreAuthorize("hasAuthority('PERMISSION_QUERY')")
    @RequestMapping("/findAuthorityIdsByRoleId")
    public Result findAuthorityIdsByRoleId(Integer id){
        try {
            List<Integer> list=roleService.findAuthorityIdsByRoleId(id);
            return new Result(true,MessageConstant.GET_ROLE_PERMISSIONS_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ROLE_PERMISSIONS_FAIL);
        }
    }


    //编辑角色信息
    @PreAuthorize("hasAuthority('ROLE_EDIT')")
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role, Integer [] authorityIds,Integer [] menuIds){
        try {
            roleService.edit(role,authorityIds,menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_ROLE_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_ROLE_SUCCESS);
    }

    @RequestMapping(value = "/checkName")
    public Result checkName(@RequestBody Map jsonData){
        try {
            String name = (String)jsonData.get("name");
            boolean isExsist= roleService.checkName(name);
            return new Result(true,"校验成功",isExsist);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"校验失败");
        }
    }

    //删除角色信息
    @PreAuthorize("hasAuthority('ROLE_DELETE')")
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            roleService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ROLE_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS);
    }
}
